export default {
  data() {
    return {
      selectList: [],
      show: false,
      showShanchu: false,
      showXiajia: false,

      pageList: [], //商品list
      currentimgList: [], //当前选择LIST
      swiperCurrent: 0, //商品状态
      stateTextid: 0, //商品分类
      pageindex: 1, //当前页面
      pagecont: 0, //总条数
      showXiajiaId: [], //商品id
    }
  },
  onLoad(suiid) {
    this.swiperCurrent = Number(suiid.stateTextid)
    this.stateTextid = Number(suiid.stateTextid)
    this.GetMyGoodsListDataList() // 列表
  },
  // 上拉加载分页
  onReachBottom() {
    this.pageindex = ++this.pageindex
    this.GetMyGoodsListDataList()
    var pagezs = (this.pagecont + 10 - 1) / 10
    if (this.pageindex >= pagezs) {
      this.status = 'nomore'
      return
    }
    this.status = 'loadmore'
    setTimeout(() => {
      if (this.pageindex >= pagezs) {
        this.status = 'nomore'
      } else {
        this.status = 'loading'
      }
    }, 1000)
  },
  methods: {
    // 车源管理接口
    async GetMyGoodsListDataList() {
      try {
        let data = {
          'GoodsState': this.swiperCurrent, //商品状态（0全部，1出售中，2已下架，3已过期）
          'Type': this.stateTextid, //分类（0全部，1渠道-对应商户端，2零售-对应消费端，3对应客户端和销售端）
          'PageIndex': this.pageindex,
          'PageSize': 10,
          'Store_Id': this.$store.state.userInfoDetail.store.storeId
        }
        const res = await this.$api.carSource.GetMyGoodsListDataList(data)
        this.pagecont = res.data.total

        // 赋值
        if (res.data.list !== null) {
          if (res.data.list === null && this.pageindex === 1) {
            this.pageList = []
            return false
          }
          if (this.pageindex === 1) {
            this.pageList = res.data.list
          } else if (this.pageindex > 1) {
            this.pageList = this.pageList.concat(res.data.list)
          }
          // 增加字段check
          let listbox = [] //暂存赋值
          this.pageList.forEach((item, i) => {
            item.check = false
            listbox.push(item)
          })
          this.pageList = listbox
          console.log('pageLIST', this.pageList)
        }
      } catch (err) {
        console.log(err)
      }
    },
    //批量选择
    selectCom(items, index) {
      this.show = false //取消全选
      for (var i = 0; i < this.pageList.length; i++) {
        if (i === index) {
          if (this.pageList[index].check === false) {
            // 选中
            this.pageList[index].check = true
            this.currentimgList.push(this.pageList[index])
            this.showXiajiaId.push(this.pageList[index].id)
            console.log(111, this.currentimgList)
            console.log(1, this.showXiajiaId)

          } else {
            this.pageList[index].check = false
            // 取消选择
            for (var j = 0; j < this.currentimgList.length; j++) {
              if (this.currentimgList[j].check === this.pageList[index].check) {
                this.currentimgList.splice(j, 1)
                this.showXiajiaId.splice(j, 1)
              }
            }
            console.log(222, this.currentimgList)
            console.log(2, this.showXiajiaId)
          }
        }
        this.$forceUpdate() // 通过强制渲染就可以了
      }
    },
    //全选
    allSelect(flag) {
      this.show = !flag
      this.currentimgList = [] //清空已选择的数据，全部赋值
      this.showXiajiaId = []
      for (var i = 0; i < this.pageList.length; i++) {
        if (this.show) {
          this.pageList[i].check = this.show
          this.currentimgList.push(this.pageList[i])
          this.showXiajiaId.push(this.pageList[i].id)
          // console.log('全选', this.currentimgList)
          // console.log(1, this.showXiajiaId)
        } else {
          this.pageList[i].check = false
          // 取消选择
          for (var j = 0; j < this.currentimgList.length; j++) {
            if (this.currentimgList[j].check === this.pageList[j].check) {
              this.currentimgList.splice(j, 1)
              this.showXiajiaId.splice(j, 1)
            }
          }
          // console.log('全取消', this.currentimgList)
          // console.log(2, this.showXiajiaId)
        }
      }
    },
    footerBtn(flag) {
      //0下架false，1删除true
      if (flag) {
        this.UpdateMyCarSourceShelves(flag)
        this.showShanchu = false
      } else {
        for (var j = 0; j < this.currentimgList.length; j++) {
          if (this.currentimgList[j].shelve_Status === 1) {
            // debugger
            this.showXiajia = false
            this.$refs.uToast.show({
              title: '包含已下架商品',
              type: 'warning',
              url: '/pages/user/index'
            })
            return
          }
        }
        this.UpdateMyCarSourceShelves(flag)
        this.showXiajia = false
      }
    },

    // 下架接口
    async UpdateMyCarSourceShelves(flag) {
      try {
        let data = {
          'Type': flag, //0下架，1删除
          'Id': this.showXiajiaId, //车辆Id
        }
        const res = await this.$api.carSource.UpdateMyCarSourceShelves(data)
        console.log(res)
        if (res.code === 200) {
          if (flag) {
            this.$refs.uToast.show({
              title: '删除成功',
              type: 'success',
              url: '/pages/user/index'
            })
          } else {
            this.$refs.uToast.show({
              title: '下架成功',
              type: 'success',
              url: '/pages/user/index'
            })
          }
          // uni.navigateTo({
          //   url: '/pages_carSource/releaseOptions/releaseOptions?type=1'
          // })
          this.pageindex = 1
          this.GetMyGoodsListDataList()
        }
      } catch (err) {
        console.log(err)
      }
    },
  }
}
